import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CityController } from './city/city.controller';
import { HomeController } from './home/home.controller';

import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigService, ConfigModule } from '@nestjs/config';
import envConfig from '../config/env';
import { PlotsEntity } from "../src/plots/plot.entity";
import { HouseEntity } from "../src/house/house.entity";
import { PlotModule } from "../src/plots/plot.module";
import { HouseModule } from './house/house.module';

import { UserEntity } from "./user/user.entity";
import { UserModule } from './user/user.module';
import { MessageEntity } from "./message/message.entity";
import { MessageModule } from './message/message.module';
@Module({
  imports: [ConfigModule.forRoot({ 
    isGlobal: true,  // 设置为全局
    envFilePath: [envConfig.path] 
   }),    
   TypeOrmModule.forFeature([PlotsEntity,HouseEntity,UserEntity,MessageEntity]),
   TypeOrmModule.forRootAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: async (configService: ConfigService) => ({
      type: 'mysql', // 数据库类型
      entities: [PlotsEntity,HouseEntity,UserEntity,MessageEntity],  // 数据表实体
      host: configService.get('DB_HOST', 'localhost'), // 主机，默认为localhost
      port: configService.get<number>('DB_PORT', 3306), // 端口号
      username: configService.get('DB_USER', 'root'),   // 用户名
      password: configService.get('DB_PASSWORD', 'yms150328'), // 密码
      database: configService.get('DB_DATABASE', 'house'), //数据库名
      timezone: '+08:00', //服务器上配置的时区
      synchronize: false,
      dropSchema: false,
      autoLoadEntities: true,
    }),
  }),
  PlotModule,
  HouseModule,
  UserModule,
  MessageModule
  ],
  controllers: [AppController, CityController, HomeController],
  providers: [AppService],
})
export class AppModule {}
